package com.top.mall.main.mine

import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.jetpack.vm.MineViewModel
import kotlinx.android.synthetic.main.activity_leader_save.*

class LeaderSaveActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(LeaderSaveActivity::class.java)
        }
    }

    private val mOptionsTime: TimePickerView by lazy {
        TimePickerBuilder(this) { date, view ->
            tvLeaderYear.text = TimeUtils.date2String(date, "yyyy-MM-dd")
        }
            .setType(
                booleanArrayOf(true, true, true, false, false, false)
            )
            .setLabel(("年"), ("月"), ("日"), ("时"), ("分"), ("秒"))
            .isCenterLabel(false)
            .isCyclic(false)
            .build()
    }

    override val layoutResId: Int
        get() = R.layout.activity_leader_save

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.colorWhite, view = flLeaderBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        tvLeaderYear.setOnClickListener {
            mOptionsTime.show()
        }

        tvLeaderSave.setOnClickListener {
            if (tvLeaderXing.text.isNullOrEmpty() || tvLeaderMing.text.isNullOrEmpty() ||
                tvAddressCard.text.isNullOrEmpty() || tvLeaderYear.text.isNullOrEmpty() ||
                tvLeaderPhone.text.isNullOrEmpty()
            ) {
                toast("请输入完整信息")
                return@setOnClickListener
            }

            viewModel.saveLeader(
                mobile = tvLeaderPhone.text.toString(),
                surname = tvLeaderXing.text.toString(),
                user_name = tvLeaderMing.text.toString(),
                id_card = tvAddressCard.text.toString(),
                birthday = tvLeaderYear.text.toString(),
                is_default = if (cbLeaderSelect.isChecked) 1 else 0
            )

        }
    }

    override fun initViewMode() {
        viewModel.saveLeaderLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg)
            finish()
        })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }
}